//
//  YXBankCardTableView.m
//  CreditCard
//
//  Created by 杨旭 on 2017/12/23.
//  Copyright © 2017年 杨旭. All rights reserved.
//

#import "YXBankCardTableView.h"
#import "YXWithdrawalTableView.h"
#import "YXBankModel.h"

@interface YXBankCardTableView ()<UITableViewDelegate,UITableViewDataSource>

@end

@implementation YXBankCardTableView



- (void)setDataArr:(NSMutableArray *)dataArr {
    _dataArr = dataArr;
    [self reloadData];
}

- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style {
    if (self = [super initWithFrame:frame style:style]) {
        self.separatorStyle = UITableViewCellSeparatorStyleNone;
        self.contentInset = UIEdgeInsetsMake(5, 0, 0, 0);
        self.dataSource = self;
        self.delegate = self;
        [self registerClass:[YXBankCardTableViewCell class] forCellReuseIdentifier:@"YXBankCardTableViewCell"];
        [self registerClass:[YXWithdrawalTableViewCell class] forCellReuseIdentifier:@"YXWithdrawalTableViewCell"];
    }
    return self;
}

#pragma mark - UITableView Delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return section == 0 ? self.dataArr.count : 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        YXBankCardTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YXBankCardTableViewCell" forIndexPath:indexPath];
        cell.model = self.dataArr[indexPath.row];
        return cell;
    }else {
        YXWithdrawalTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YXWithdrawalTableViewCell" forIndexPath:indexPath];
        cell.titleLab.text = @"添加银行卡";
        cell.contentLab.hidden = YES;
        UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"箭头_001"]];
        cell.accessoryView = image;
        return cell;
    }
    return [[UITableViewCell alloc] init];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return indexPath.section == 0 ? 110 : 50;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        YXBankModel *model = self.dataArr[indexPath.row];
        if (self.selectBankCardNumberBlock) {
            self.selectBankCardNumberBlock(model);
        }
    }else {
        if (self.addBankCardClickBlock) {
            self.addBankCardClickBlock();
        }
        
    }
    
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}

- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        UITableViewRowAction *dele = [UITableViewRowAction rowActionWithStyle:(UITableViewRowActionStyleDestructive) title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
            
            YXBankModel *model = self.dataArr[indexPath.row];
            self.delAccountBlock(model.ID);
            [self.dataArr removeObjectAtIndex:indexPath.row];
            [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationAutomatic)];
            [self reloadData];
        }];
        return @[dele];
    }
    return nil;
}


@end

@interface YXBankCardTableViewCell ()

@end

@implementation YXBankCardTableViewCell

- (UIView *)backView {
    if (!_backView) {
        _backView = [[UIView alloc] initWithFrame:(CGRectMake(10, 5, kScreenWidth - 20, 100))];
        _backView.backgroundColor = [UIColor py_colorWithHexString:@"32A1F6"];
        _backView.layer.masksToBounds = YES;
        _backView.layer.cornerRadius = 10.0f;
        _backView.layer.shadowColor = [UIColor blackColor].CGColor;
        _backView.layer.shadowOffset = CGSizeMake(-3, -3);
        _backView.layer.shadowOpacity = 0.2;
        _backView.layer.shadowRadius = 5;
    }
    return _backView;
}

- (UILabel *)titleLab {
    if (!_titleLab) {
        _titleLab = [UILabel setLabelWithText:@"交通银行" fontSize:18 color:[UIColor whiteColor]];
    }
    return _titleLab;
}

- (UILabel *)typeLab {
    if (!_typeLab) {
        _typeLab = [UILabel setLabelWithText:@"储蓄卡" fontSize:14 color:[UIColor whiteColor]];
    }
    return _typeLab;
}

- (UILabel *)cardNumberLab {
    if (!_cardNumberLab) {
        _cardNumberLab = [UILabel setLabelWithText:@"**** **** **** 9999" fontSize:20 color:[UIColor whiteColor]];
        _cardNumberLab.frame = CGRectMake(0, 55, self.backView.W, 20);
        _cardNumberLab.textAlignment = NSTextAlignmentCenter;
        _cardNumberLab.adjustsFontSizeToFitWidth = YES;

    }
    return _cardNumberLab;
}

- (UIView *)selectView {
    if (!_selectView) {
        _selectView = [[UIView alloc] init];
        _selectView.layer.borderColor = [UIColor whiteColor].CGColor;
        _selectView.layer.borderWidth = 1.0f;
        _selectView.layer.masksToBounds = YES;
        _selectView.layer.cornerRadius = 5;
    }
    return _selectView;
}

- (void)setModel:(YXBankModel *)model {
    _model = model;
    _titleLab.text = _model.bankName;
//    _cardNumberLab.text = [UILabel formateBankNum:_model.bankNo];
    
    _cardNumberLab.attributedText = [self bankCardNoAttriStr];

}


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.backgroundColor = [UIColor whiteColor];
        [self.contentView addSubview:self.backView];
        [self.backView addSubview:self.titleLab];
        [self.backView addSubview:self.typeLab];
        [self.backView addSubview:self.cardNumberLab];
        [self.backView addSubview:self.selectView];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    YXWeakSelf
    [_titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.backView.mas_left).offset(20);
        make.top.equalTo(weakSelf.backView.mas_top).offset(10);
        [_titleLab sizeToFit];
    }];
    
    [_typeLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.backView.mas_left).offset(20);
        make.top.equalTo(weakSelf.titleLab.mas_bottom).offset(0);
         [_typeLab sizeToFit];
    }];
    
//    [_cardNumberLab mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(weakSelf.backView.mas_left).offset(0);
//        make.right.equalTo(weakSelf.backView.mas_right).offset(0);
//        make.top.equalTo(weakSelf.titleLab.mas_bottom).offset(20);
//        [_cardNumberLab sizeToFit];
//    }];
    
    [_selectView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.backView.mas_top).offset(10);
        make.right.equalTo(weakSelf.backView.mas_right).offset(-10);
        make.size.mas_equalTo(CGSizeMake(10, 10));
    }];
    
}

-(NSMutableAttributedString *)bankCardNoAttriStr
{
    NSString *str = _model.bankNo;
    NSString *bankcardnoSub = [str substringFromIndex:str.length- 4];
    NSMutableAttributedString *attriStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"＊＊＊＊ ＊＊＊＊ ＊＊＊＊ %@",bankcardnoSub]];
    [attriStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15.f] range:[attriStr.string rangeOfString:@"＊＊＊＊ ＊＊＊＊ ＊＊＊＊"]];
    [attriStr addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:20.f] range:[attriStr.string rangeOfString:bankcardnoSub]];
    return attriStr;
}


@end


